define(['jquery', 'cm'], function () {
    $('footer').load('../html/footer.html')
    class Login {
        constructor() {
            this.userName = document.querySelector('#recipient-name')
            this.passWord = document.querySelector('#message-text')
            this.log = document.querySelector('#login')
            this.out = document.querySelector('#out')
            this.getBack = document.querySelector('#getBack')
            this.checkBox = document.querySelector('#blankCheckbox')
            this.agree = document.querySelector('.agree input')
            console.log(this.agree.checked)
            this.getNumber()
            this.go()
            this.bactTo()
            this.charge()
        }
        getNumber() {
            return JSON.parse(localStorage.getItem('curt')) || []
        }
        go() {
            var arr = this.getNumber()
            // console.log(arr)
            this.log.addEventListener('click', () => {
                // console.log(this.userName.value)
                console.log(arr)
                var a = arr.find(item => {
                    return item.userName == this.userName.value && item.password == this
                        .passWord.value
                });
                console.log(a)
                // console.log(a)
                if (a && this.agree.checked) {
                    alert('登录成功')
                    window.location.href = 'welcome.html'
                    // delCookie('用户')
                    setCookie('用户', a.userName, 1000)
                    setCookie('头像', a.src, 1000)
                    if (this.checkBox.checked) {
                        setCookie('密码', a.password, 1000)
                    }
                } else if (!a) {
                    alert('密码或账号不正确')
                }
                else {
                    alert('请勾选协议')
                }
            })
            this.out.addEventListener('click', () => {
                if (getCookie('用户')) {
                    alert('您成功退出登录')
                    delCookie('用户')
                    window.location.href = '../index.html'
                }
                else {
                    alert('您现在还没有登录噢~')
                }
            })

        }
        bactTo() {
            this.getBack.addEventListener('click', () => {
                self.location = 'register.html'
            })
        }
        charge() {
            if (getCookie('密码')) {
                this.userName.value = getCookie('用户')
                this.passWord.value = getCookie('密码')
                delCookie('密码')
            } else {
                this.userName.value = ''
                this.passWord.value = ''

            }
        }
    }
    new Login()

})